<?php
declare (strict_types = 1);

namespace app\admin\controller;
use think\facade\Cache;  //缓存命名空间
use \think\facade\Db;
use \think\facade\Request;
use \think\facade\View;
class Good
{
    public function cate()
    {
        $pid=input('id',0);
        $t = \think\facade\Db::name('category')
            ->where('parentid',$pid)
            ->order('sort')
            ->select();
        $t=$t->toArray();
//        foreach ($t as &$v){
//            $v['next'] = Db::name('category')
//                ->where('parentid',$v['id'])
//                ->count();
//        }

        //缓存  has 判断  set 设置  get 获取
        //参数  set(缓存变量名,变量,失效时间)

        //判断缓存是否设置
        if(Cache::has('cate')){
            //获取缓存信息
            $all = Cache::get('cate');
        }else{
            $all = Db::name('category')
                ->distinct(true)     //去重
                ->column('parentid');  //获取所有pid
            //存储进缓存文件中
            Cache::set('cate',$all,10);
        }
        //$all = Db::name('category')->column('parentid');
        foreach ($t as &$v){
            //判断当前分类 id 在所有分类父 id 中出现 则有下一级
            if(in_array($v['id'],$all)){
                $v['next'] = 1;
            }else{
                $v['next'] = 0;
            }
        }
        View::assign('t',$t);

        return view();

//        return View::fetch();
    }
    public function good_add()
    {
        return view();
//        return View::fetch();
    }
    public function good_list()
    {
        //接 post 数据所有的值
        $post= Request::post();
        //批量赋值 可以按键输出 进行留值
        View::assign($post);
        //拼装搜索条件
        $where=[];
//        echo "<pre>";
//        print_r($post);
//        echo "</pre>";
        if(isset($post['keywords']) && $post['keywords'] <> '')       $where[] = ['g.name',    'like',   "%{$post['keywords']}%"];
        if(isset($post['is_down'])  && count($post['is_down']) == 1)  $where[] = ['g.is_down', 'in',     $post['is_down']];
        if(isset($post['is_show']) && $post['is_show'] <> '')         $where[] = ['g.is_show', '=',      $post['is_show']];
        if(isset($post['is_hot']) && $post['is_hot'] <> '')           $where[] = ['g.is_hot',  '=',      $post['is_hot']];

        $t = Db::name('goods')
            ->alias('g')
            ->join(' category c','g.cid = c.id','left')
            ->field('g.id,g.name,g.photo_x,g.price,g.addtime,g.is_show,g.is_down,g.is_hot,g.content')
            ->field('c.name cname')
            ->where($where)
            ->select();

        //取址符  可以改变循环源的内容
        View::assign('t',$t);
        return view();
//        return View::fetch();
    }

    public function good_brand()
    {
        $t = \think\facade\Db::name('brand')->select();
        View::assign('t',$t);
        return view();
//        return View::fetch();
    }

}
